Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): default to group when no options.session #196

Closed
wants to merge 1 commit into from

Conversation

ilharp
Copy link
Contributor

@ilharp ilharp commented Dec 12, 2023

对于 createMessage API,判断在私聊还是群聊中发送是适配器的职责。在判断出私聊还是群聊后,适配器需要在 MessageEncoderprepare() 方法中做如下五件事:

  1. 设置 this.session.isDirect,该值在被适配器设置之前始终为默认值
  2. this.session.channel.type = this.session.isDirect ? Channel.Type.DIRECT : Channel.Type.TEXT
  3. this.session.subtype = this.session.isDirect ? 'private' : 'group'
  4. 如果 Bot 对应的平台为单层架构(无 Guild 概念),则 if (!this.session.isDirect) this.guildId = this.channelId
  5. 修正所有 flush() 及同类方法中的实际消息发送逻辑,使其基于 this.session.isDirect 判断需要调用的 API

adapter-onebot#8 是上面修改的一个完美示范。


对于本 PR,一般地,若 createMessage 请求没有来源 session,且适配器也不打算进行上述三项设置的情况下,我们认为此时应当默认发送群聊消息。这一结论根据目前的用户群体在私聊和群聊中使用该 API 的比例得出。本 PR 规范了这一行为。

本 PR 是一个中间改动。在所有适配器均实现了上面所述的判断逻辑后,本 PR 中 send() 方法中 isDirect 相关部分将会全部失效,届时将可以直接删除相关行。

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (bfc3dc8) 86.08% compared to head (4e8a083) 86.08%.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #196   +/-   ##
=======================================
  Coverage   86.08%   86.08%           
=======================================
  Files           1        1           
  Lines         503      503           
  Branches      111      111           
=======================================
  Hits          433      433           
  Misses         70       70           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ilharp ilharp marked this pull request as ready for review December 20, 2023 13:03
ilharp added a commit to koishijs/koishi-plugin-adapter-onebot that referenced this pull request Dec 23, 2023
@ilharp
Copy link
Contributor Author

ilharp commented Dec 30, 2023

经过讨论,修改上面的逻辑如下:

  1. 新增抽象方法,用于各适配器从 channelId 判断返回 Channel.Type
  2. this.session.isDirect 修改为 this.session.channel.type 的计算属性: this.session.channel.type === Channel.Type.DIRECT
  3. subtype 彻底删除
  4. if (!this.session.isDirect) this.guildId = this.channelId 保留
  5. 改为基于 this.session.channel.type 判断需要使用的 API,由此支持「向语音频道发送文本消息」功能

@shigma
Copy link
Contributor

shigma commented Jan 8, 2024

superseded by #216

@shigma shigma closed this Jan 8, 2024
@ilharp
Copy link
Contributor Author

ilharp commented Feb 20, 2024

再次说明一下适配器要做的事:

适配器 MessageEncoder 中的 prepare() 方法中

  1. this.session.event.channel.type 为空时根据 this.session.event.channel.id 判断出 this.session.event.channel.type 并填充
  • this.session.event.channel.type 存在时不执行任何操作
  • 判断方法需要根据平台特征实现,如 onebot 可以使用 this.session.event.channel.id.startsWith('private:') 判断
  1. this.session.isDirect = this.session.event.channel.type === Channel.Type.DIRECT

  2. this.session.subtype = this.session.isDirect ? 'private' : 'group'

  3. 如果 Bot 对应的平台为单层架构则 if (!this.session.isDirect) this.guildId = this.channelId

例:QQGroupBot 需要写这句话而 QQChannelBot 不需要

适配器 MessageEncoder

  1. 修正所有 flush() 及同类方法中的实际消息发送逻辑,使其基于 this.session.event.channel.type 判断需要调用的 API

目前多数官方和社区适配器均未完整实现此逻辑,例:

  1. 仍在内部使用 this.session.isDirect 而非 this.session.event.channel.type
  2. 没有在 prepare() 方法中填充 this.session.event.channel 的所有可填属性

这会给 Satori 未来对子频道和语音频道发送消息的支持埋下一些隐患。各适配器可以闲时检查并保证上述逻辑。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants